#include "stdafx.h"

#include <time.h>

DumbAgent::DumbAgent(tank_t* tank, BZRC* myTeam){
	this->myTeam = myTeam;
	this->tank = tank;
	nextTime = GetTime();
	shotTime = GetTime();
	currentState = 0;
	srand(time(NULL));
	
}
void DumbAgent::DoAction(){
	long currentTime = GetTime();
	
	if(currentTime >= nextTime){
		//cout << currentTime << " " << nextTime << endl;
		//default state the drone needs to move forward
		if(currentState == 0){
			cout << "accelerating for ";
			Accelerate(currentTime);
		}
		//the drone is moving
		else if(currentState & 1){
			//the drone has "stopped"
			if(currentState & 2){
				Turn(currentTime);
				cout << "turning\n";
			}
			//the drone needs to stop
			else{
				Stop(currentTime);
				cout << "stopping\n";
			}
		}
		//the drone is turning
		else if(currentState & 4){
			StopTurning(currentTime);
			cout << "stop turning\n";
		}
	}
	//the drone needs to shoot
	if(currentTime >= shotTime){
		Shoot(currentTime);
	}
}

void DumbAgent::Accelerate(long currentTime){
	myTeam->speed(tank->index, 1.0);
	long r = (rand() % 5001 + 2000);
	cout << r << endl;
	nextTime = currentTime + r;
	currentState = 1;
}

void DumbAgent::Stop(long currentTime){
	myTeam->speed(tank->index, 0.0);
	nextTime = currentTime + 1000;
	currentState = 3;
}

void DumbAgent::Turn(long currentTime){
	myTeam->angvel(tank->index, 1.0);
	nextTime = currentTime + 1750;
	currentState = 4;
}

void DumbAgent::StopTurning(long currentTime){
	myTeam->angvel(tank->index, 0.0);
	nextTime = currentTime + 1000;
	currentState = 0;
}

void DumbAgent::Shoot(long currentTime){
	myTeam->shoot(tank->index);
	int r = (rand() % 10 + 15) * 100;
	shotTime = currentTime + r;
}

long DumbAgent::GetTime(){
	SYSTEMTIME tm;
	GetSystemTime(&tm);
	return (long)tm.wMilliseconds + 
		   (long)tm.wSecond * 1000 + 
		   (long)tm.wMinute * 60000 + 
		   (long)tm.wHour * 3600000;// + 
		   //(long)tm.wDay * 86400000;
}
